#include <stdlib.h>

#include "Deque.h"

static size_t bufSize() {
	return 512 / sizeof(int);
}

static void dequeIteratorConstruct(DequeIterator *_this, int **_node, int *_current);

static void dequeIteratorDestruct(DequeIterator *_this);

static void dequeIteratorSetNode(DequeIterator *_this, int **_node);

static void dequeIteratorConstruct(DequeIterator *_this, int **_node, int *_current) {
	dequeIteratorSetNode(_this, _node);
	_this->current_ = _current;
}

static void dequeIteratorDestruct(DequeIterator *_this) {
}

int *dequeIteratorData(DequeIterator *_this) {
	return _this->current_;
}

const int *dequeIteratorDataConst(const DequeIterator *_this) {
	return _this->current_;
}

DequeIterator *dequeIteratorIncrease(DequeIterator *_this) {
	++_this->current_;
	if (_this->current_ == _this->last_) {
		dequeIteratorSetNode(_this, _this->node_ + 1);
		_this->current_ = _this->first_;
	}
	return _this;
}

DequeIterator dequeIteratorIncreaseOne(DequeIterator *_this) {
	DequeIterator temp = *_this;
	dequeIteratorIncrease(_this);
	return temp;
}

DequeIterator *dequeIteratorDecrease(DequeIterator *_this) {
	if (_this->current_ == _this->first_) {
		dequeIteratorSetNode(_this, _this->node_ - 1);
		_this->current_ = _this->last_;
	}
	--_this->current_;
	return _this;
}

DequeIterator dequeIteratorDecreaseOne(DequeIterator *_this) {
	DequeIterator temp = *_this;
	dequeIteratorDecrease(_this);
	return temp;
}

static void dequeIteratorSetNode(DequeIterator *_this, int **_node) {
	_this->node_ = _node;
	_this->first_ = *_node;
	_this->last_ = _this->first_ + bufSize();
}
